<?php

namespace App\Models;

class Demands extends Base
{
    protected $table = 'demands';

    const BID_1 = 1;
    const BID_2 = 2;
    const BID_3 = 3;
    const BID_4 = 4;
    const BID_TEXT = [
        self::BID_1 => '<span class="el-tag">未投标</span>',
        self::BID_2 => '<span class="el-tag el-tag--danger">投标中</span>',
        self::BID_3 => '<span class="el-tag el-tag--success">已中标</span>',
        self::BID_4 => '<span class="el-tag el-tag--warning">已完成</span>',
    ];

    const EVALUATE_STATUS_1 = 1;
    const EVALUATE_STATUS_2 = 2;
    const EVALUATE_STATUS_TEXT = [
        self::EVALUATE_STATUS_1 => '<span class="el-tag">否</span>',
        self::EVALUATE_STATUS_2 => '<span class="el-tag el-tag--danger">是</span>',
    ];

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">未通过</span>',
        self::STATUS_4 => '<span class="el-tag el-tag--info">下架</span>',
    ];

    protected $fillable = [
        'id','user_id','user_name','title','cate_pid','cate_id','content','area','price','link_name','link_phone','link_department',
        'link_position','company_name','company_type','company_size','company_industry','other','number','limit_num','bid_costs',
        'service_charge','view','pay_at','is_can','end_at','urgent','type','way','bid','evaluate_content','evaluate_contract_pdf',
        'evaluate_contract_img','evaluate_status','evaluate_at','status','created_at','updated_at','service_cate'
    ];

    protected $casts = ['evaluate_contract_img' => 'array'];

    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_id', 'id');
    }
    public function area(){//区域
        return $this->belongsTo('App\Models\Areas', 'area', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%')
                        ->orwhere('link_name','like','%' . $params['kwd'] . '%')
                        ->orwhere('company_name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['bid']) && $params['bid']) {
                    $query ->where('bid',$params['bid']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->with(['cate_id' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['cate_pid' => function ($query) {
                $query->select(['id','name'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('price',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'bid'=> self::BID_TEXT,
            'evaluate_status'=> self::EVALUATE_STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
//        $params['status'] = 1;
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if (isset($params['bid']) && $params['bid']){
                        if($params['bid'] == 3){
                            $query ->whereRaw('(bid=3 OR bid=4)');
                        }else{
                            $query ->where('bid',$params['bid']);
                        }
                    }
                    if(isset($params['urgent']) && $params['urgent']) {
                        $query ->where('urgent',$params['urgent']);
                    }
                    if(isset($params['area']) && $params['area']) {
                        $query ->where('area',$params['area']);
                    }
                    if(isset($params['way']) && $params['way']) {
                        $query ->where('way',$params['way']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->whereIn('cate_id',$params['cid_id']);
                    }
                    if(isset($params['price_one']) && $params['price_one']) {
                        $query ->where('price','>',$params['price_one']);
                    }
                    if(isset($params['price_two']) && $params['price_two']) {
                        $query ->where('price','<=',$params['price_two']);
                    }
                    if(isset($params['start_time']) && $params['start_time']) {
                        $query ->whereDate('end_at','>=',$params['start_time']);
                    }
                    if(isset($params['end_time']) && $params['end_time']) {
                        $query ->whereDate('end_at','<=',$params['end_time']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%");
                    }
                })->where('bid','<>',1)->where('status','<>',-1)->where('status','<>',4)
                ->orderBy('is_can', 'asc')->orderBy('bid', 'asc')->orderBy('urgent', 'desc')->orderBy($sort, $orderby)
                ->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if (isset($params['bid']) && $params['bid']){
                        if($params['bid'] == 3){
                            $query ->whereRaw('(bid=3 OR bid=4)');
                        }else{
                            $query ->where('bid',$params['bid']);
                        }
                    }
                    if(isset($params['urgent']) && $params['urgent']) {
                        $query ->where('urgent',$params['urgent']);
                    }
                    if(isset($params['area']) && $params['area']) {
                        $query ->where('area',$params['area']);
                    }
                    if(isset($params['way']) && $params['way']) {
                        $query ->where('way',$params['way']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->whereIn('cate_id',$params['cid_id']);
                    }
                    if(isset($params['price_one']) && $params['price_one']) {
                        $query ->where('price','>',$params['price_one']);
                    }
                    if(isset($params['price_two']) && $params['price_two']) {
                        $query ->where('price','<=',$params['price_two']);
                    }
                    if(isset($params['start_time']) && $params['start_time']) {
                        $query ->whereDate('end_at','>=',$params['start_time']);
                    }
                    if(isset($params['end_time']) && $params['end_time']) {
                        $query ->whereDate('end_at','<=',$params['end_time']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%");
                    }
                })->where('bid','<>',1)->where('status','<>',-1)->where('status','<>',4)
                ->orderBy('is_can', 'asc')->orderBy('bid', 'asc')->orderBy('urgent', 'desc')->orderBy($sort, $orderby)
                ->limit($limit)->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }


    /**
     * 个人中心获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function centerList($params = [], $fields = ['*'], $page=1, $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if (isset($params['bid']) && $params['bid']){
                    if($params['bid'] == 3){
                        $query ->whereRaw('(bid=3 OR bid=4 OR is_can =2)');
                    }else{
                        $query ->where('bid',$params['bid']);
                        $query ->where('is_can',1);
                    }
                }
            })
            ->where('status','<>',-1)
            ->orderBy('created_at', 'desc')->paginate($limit, null, null, $page);
        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params = [], $fields = ['*']){
        return $this->select($fields)->where($params)->where('status','<>',-1)
            ->with(['area' => function ($query) {
                $query->select(['id','name'])->get();
            }])->first();
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
        $fields = [ 'user_name','title','content','area','price','end_at','link_name','link_phone','link_department',
            'link_position','company_name','company_type','company_size','company_industry','limit_num','number',
            'bid_costs','service_cate','service_charge','pay_at','urgent','created_at' ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->get();
        foreach ($lists as $k=>$v){
            $v->attributes['area'] = Areas::where('id',$v['area'])->where('status','<>',-1)->value('name');
            $v->attributes['company_type'] = CompanyInfos::where('id',$v['company_type'])->value('name');
            $v->attributes['company_industry'] = CompanyInfos::where('id',$v['company_industry'])->value('name');
            $v->attributes['company_size'] = CompanyInfos::where('id',$v['company_size'])->value('name');
            if($v->attributes['service_cate'] == 1){
                $v->attributes['service_cate'] = '来电佣金';
            }elseif ($v->attributes['service_cate'] == 2){
                $v->attributes['service_cate'] = '投标佣金';
            }else{
                $v->attributes['service_cate'] = '指定中标';
            }
            if($v->attributes['urgent'] == 1){
                $v->attributes['urgent'] = '否';
            }else{
                $v->attributes['urgent'] = '是';
            }
            if ($v['number'] == $v['limit_num'] || trim(@date('Y-m-d', strtotime($v['end_at']))) < date('Y-m-d')){
                $end_state = 1;//已过截止日期或者投标人数已满，不能再投标
            }else{
                $end_state = 0;//可以投标
            }
        }
        return $lists;
    }

}
